﻿@model IEnumerable<Commission.Models.TransactionHistory>
@using MyResources = Commission.Resources.AppResource
@using Microsoft.AspNet.Identity
@using Commission.Models;

@{
    ViewBag.Title = @MyResources.LinkWithdrawList;
}

@section MyJavascript {
    <script type="text/javascript">
        $(function () {
            $('#UserName').autocomplete(
            {
                source: '@Url.Action("CustomerSearch", "Customer")'
            }
            );
        });
    </script>
}

@section Sortable {
    @Scripts.Render("~/bundles/sortable")
}

<script type="text/javascript">
    // chỉ chọn những dòng có tình trạng là chưa rút tiền
    function getTransactionId() {
        var Ids = "";

        $('#list tr').each(function (i, row) {
            var thisRow = $(this);

            var transId = thisRow.closest('tr').children('input.transId').val();

            thisRow.find('input:checkbox:checked').each(function () {
                if (transId != undefined) {
                    Ids = Ids + transId + "_";
                }
            });
        });

        if (Ids == "") {
            alert("Hãy chọn dòng chi tiết apply rút tiền.");
        }

        return Ids;
    }

    // chọn toàn bộ danh sách trên màn hình ko quan tâm tới tình trạng rút tiền
    function getExportTransactionIds() {
        var Ids = "";

        $('#list tr').each(function (i, row) {
            var thisRow = $(this);

            var transId = thisRow.closest('tr').children('input.exportTransId').val();

            thisRow.find('input:checkbox:checked').each(function () {
                if (transId != undefined) {
                    Ids = Ids + transId + "_";
                }
            });
        });

        if (Ids == "") {
            alert("Hãy chọn dòng chi tiết.");
        }

        return Ids;
    }

    $(document).ready(function(){
        $("#updateWithdraw").click(function (event) {
            var ids = getTransactionId();
            if (ids == "") return false;

            $(this).attr('href',
                "WithdrawList?UpdateTransactionIds=" + ids);
        });

        $("#cancelWithdraw").click(function (event) {
            var ids = getTransactionId();
            if (ids == "") return false;

            $(this).attr('href',
                "WithdrawList?CancelTransactionIds=" + ids);
        });

        $("#exportToExcel").click(function (event) {
            var ids = getExportTransactionIds();
            if (ids == "") return false;

            $(this).attr('href', "ExportToExcel?transactionIds=" + ids);
        });

        $('#selectAll').click(function (e) {
            //var table = $(e.target).closest('table');
            //$('#list td input:checkbox').prop('checked', this.checked);
            var checked = this.checked;

            $('#list tr').each(function (i, row) {
                var thisRow = $(this);

                //var transId = thisRow.closest('tr').children('input.transId').val();

                //if (transId === undefined) {
                //} else {
                thisRow.find('input:checkbox').prop('checked', checked);
                //}
            });
        });
    });
</script>

<h2>@MyResources.LinkWithdrawList</h2>

@using (Html.BeginForm("WithdrawList", "Transaction", FormMethod.Get))
{
    <table style="overflow:hidden">
        <tr>
            <td width="166px">
                @MyResources.LabelDateFrom :
                @Html.JQueryUI().Datepicker("dateFrom", ViewBag.DateFrom,
                    new { @class = "form-control-date searchtext", @style = "width:88px" })
            </td>

            <td width="170px">
                @MyResources.LabelDateTo :
                @Html.JQueryUI().Datepicker("dateTo", ViewBag.DateTo,
                    new { @class = "form-control-date searchtext", @style = "width:88px" })
            </td>

            <td width="232px">
                @MyResources.LabelCustomer :
                @Html.TextBox("UserName", ViewBag.CurrentFilter as string,
                    new { @class = "form-control searchtext ", @style = "width:160px" })
            </td>
            <td>
                @MyResources.LabelNotReceivedMoneyYet :
            </td>

            <td width="40px" class="text-center">
                @Html.CheckBox("IsDelayed", true, new { @class = "form-control-checkbox" })
            </td>

            <td>
                <input type="submit" value="@MyResources.LabelSearch" style="height:28px" />

                @if (User.IsInRole("Admin"))
                {
                    @Html.Raw("　")
                    @Html.ActionLink(@MyResources.LinkReceivedMoney, "",
                        new { TransactionIds = "" }, new { @id = "updateWithdraw" })
                }

                @Html.Raw("　")
                @Html.ActionLink(@MyResources.LinkCancelWithdraw, "",
                        new { TransactionIds = "" }, new { @id = "cancelWithdraw" })

                @Html.Raw("　")
                @Html.ActionLink(@MyResources.LinkExportToExcel, "ExportToExcel",
                        new { id = 0 }, new { @id = "exportToExcel" })
            </td>
        </tr>
    </table>
}

<div style="height:5px;">
</div>

<table id="list" class="table hoverTable sortable">
    <tr>
        <th class="text-center" style="width:40px" title="Chọn tất cả các dòng đang apply">
            @Html.CheckBox("selectAll")
        </th>

        <th class="text-center" style="width:150px">
            @MyResources.LabelWithdrawDate
        </th>

        @if (User.IsInRole("Admin"))
        {
            <th class="text-left" style="width:220px;">
                @MyResources.LabelCustomer
            </th>
        }

        <th class="text-center" style="width:140px;">
            @MyResources.LabelWithdrawAmount
        </th>

        <th class="text-center" style="width:130px">
            @MyResources.LabelTransactionStatus
        </th>

        <th class="text-center" style="width:150px">
            @MyResources.LabelReceivedCancelDate
        </th>

        <th class="text-center">
            @MyResources.LabelDescription
        </th>

        <th class="text-left" style="width:100px;"></th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td class="text-center">
                @if (item.IsReadOnly)
                {
                    @Html.CheckBoxFor(modelItem => item.Checked, new { @disabled = "disabled" })
                } else {
                    @Html.CheckBoxFor(modelItem => item.Checked)
                }
            </td>

            <td class="text-center">
                @Html.DisplayFor(modelItem => item.DisplayCreatedAt)
            </td>

            @if (User.IsInRole("Admin"))
            {
                <td class="text-left">
                    @Html.DisplayFor(modelItem => item.Customer.DisplayName)
                </td>
            }

            @if (item.TransactionStatus == TransactionStatus.Delay)
            {
                <td class="text-right DelayWithdrawAmount">
                    @Html.Raw(string.Format("{0:#,0}", item.Amount))
                </td>
            }
            else
            {
                <td class="text-right">
                    @Html.Raw(string.Format("{0:#,0}", item.Amount))
                </td>
            }

            <td>
                @if (item.TransactionStatus == TransactionStatus.Delay)
                {
                    @Html.Raw("Đang apply")
                }
                else if (item.TransactionStatus == TransactionStatus.Cancel)
                {
                    @Html.Raw("Đã hủy rút tiền")
                }
                else if (item.TransactionStatus == TransactionStatus.Finish)
                {
                    @Html.Raw("Đã nhận tiền")
                }
            </td>

            <td class="text-center">
                @if (item.TransactionStatus != TransactionStatus.Delay) {
                    @Html.DisplayFor(modelItem => item.DisplayUpdatedAt)
                }
            </td>

            <td>
                @Html.DisplayFor(modelItem => item.Description)
            </td>

            <td>
                @Html.ActionLink(@MyResources.LinkDetail, "Details", new { id = item.ID })
            </td>

            @if (item.TransactionStatus == TransactionStatus.Delay) {
                @Html.HiddenFor(modelItem => item.ID, new { @class = "transId" })
            }
            @Html.HiddenFor(modelItem => item.ID, new { @class = "exportTransId" })
            @Html.HiddenFor(modelItem => item.TransactionStatus, new { @class = "transStatus" })
        </tr>
    }

    <tr>
        <th>
        </th>

        @if (User.IsInRole("Admin"))
        {
            <th>
            </th>
        }
        
        <th class="text-right">
            Tổng
        </th>
        <th class="text-right">
            @Html.Raw(string.Format("{0:#,0}", @Model.Sum(i => i.Amount)))
        </th>

        <th>
        </th>

        <th class="text-right">
        </th>

        <th>
        </th>

        <th></th>
    </tr>

</table>

